odoo.define('export.excel.tree', function (require) {
    "use strict";
    var ListController = require('web.ListController');
    var ListView = require('web.ListView');
    var viewRegistry = require('web.view_registry');
    var framework = require('web.framework');
     var core = require('web.core');
    var rpc = require("web.rpc");
    var _t = core._t;
    var ExportListController = ListController.extend({
        buttons_template: 'custom.export.buttons',
        events: _.extend({}, ListController.prototype.events, {
            'click .o_button_custom_export': '_onButtonSend',
        }),
        init: function (parent, model, renderer, params) {
            this.searchWhere = renderer.state.domain;
            console.log("-------------------------")
            return this._super.apply(this, arguments);
        },
        _onButtonSend: function () {
            var self = this;
            var actived_ids = [];
            var state = self.model.get(self.handle, {raw: true});
            for (var i = 0; i < $('tbody .o_list_record_selector input').length; i++) {
                if ($('tbody .o_list_record_selector input')[i].checked === true) {
                    actived_ids.push(state.res_ids[i]);
                }
            }
            var ctx = state.context;
            ctx['active_ids'] = actived_ids;
            var ids = actived_ids;
            rpc.query({
                model: 'excel.report.config',
                method: 'check_is_export',
                kwargs: {"model_name":self.modelName},
            }).then(function (data) {
                self.do_notify("提示", "请等待，正在导出Excel数据中！");
                return data;
            }).then(function (result) {
                 if (result) {
                     self.getSession().get_file({
                         complete: framework.unblockUI,
                          url: '/down/export_excel_file',
                         data: {
                             data: JSON.stringify({
                                 'model_name': self.modelName,
                                 'search_where': self.searchWhere,
                             })
                         },
                         error: (error) => self.call('crash_manager', 'rpc_error', error),

                     });

                 }
                 else {
                     self.do_notify("警告", "请联系管理员，该模块没有配置导出模板");
                 }
            });

        }
    });
    var ExcelExportListView = ListView.extend({
        config: _.extend({}, ListView.prototype.config, {
            Controller: ExportListController,
        }),
    });

    viewRegistry.add('export_excel_tree', ExcelExportListView);
});